package com.gigmissions.client;

import com.extjs.gxt.ui.client.Registry;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.ControlAnchor;
import com.google.gwt.maps.client.control.ControlPosition;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.GeoXmlOverlay;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Gwtgigmissions implements EntryPoint {
	
	 private static int MAP_BORDER = 5;

	 private MapWidget map;
	 private GeoXmlOverlay geoXml = null;
	 
	 public static final String VENUE_SERVICE = "venueservice";
	 
	 
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
			registerServices();
	
			
			FlowPanel mainPanel = new FlowPanel();
		 	LatLng centerpoint = LatLng.newInstance(39.509,-98.434);
		    
		 	// Open a map centered on Cawker City, KS USA

		    map = new MapWidget(centerpoint, 2);
		    map.setUIToDefault();
		    
		    
		    //String boundsParams = getBoundsRequestParams(map);
		    //KmlGigLoadCallback gigOverlayCallBack = new KmlGigLoadCallback(map,geoXml);
		    //GeoXmlOverlay.load(GWT.getHostPageBaseURL() + "getgig.kml?" + boundsParams + "&f=kml",gigOverlayCallBack);

		    HorizontalPanel mapPanel = new HorizontalPanel();
		    mapPanel.setWidth("100%"); 
		    mapPanel.setHeight("100%");
		    mapPanel.add(map); 
		    mapPanel.setSpacing(MAP_BORDER);

		    //Set the size of the map relative to the window 
		    resizeMap(Window.getClientWidth(),Window.getClientHeight()); 
		    Window.addResizeHandler(new ResizeHandler() {
		        public void onResize(ResizeEvent event) {
		        	resizeMap(event.getWidth(), event.getHeight()); 
		        }
		     }
		    );
		    
		    /*check for resizes
		    LatLng center = map.getCenter();
		    map.checkResize();
		    map.setCenter(center);
		    */
		    
		    
		    map.addControl(new AddGigControl(new ControlPosition(ControlAnchor.TOP_LEFT, 50, 10)));
		    map.addControl(new AddVenueControl(new ControlPosition(ControlAnchor.TOP_LEFT, 125, 10)));
			    
		       
	    
		    // Add the map to the HTML host page
		    mainPanel.setHeight("100%");
		    mainPanel.setWidth("100%");
		    		
		    //mainPanel.add(toolPanel);
		    mainPanel.add(mapPanel);
		    
		    RootPanel.get("mapContainer").add(mainPanel);
		  
		
	}
	
	
	
	
	private void registerServices(){
		
		registerPhotoService();
		
	}
	
	
	private void registerPhotoService(){
		
		 VenueServiceAsync service = (VenueServiceAsync) GWT.create(VenueService.class);
		    ServiceDefTarget endpoint = (ServiceDefTarget) service;
		    String moduleRelativeURL = VENUE_SERVICE;
		    endpoint.setServiceEntryPoint(moduleRelativeURL);
		    Registry.register(VENUE_SERVICE, service);
		
		
	}
	
		
	 private String getBoundsRequestParams(MapWidget map){
		 
		 double swLat = map.getBounds().getSouthWest().getLatitude();
		 double swLng = map.getBounds().getSouthWest().getLongitude();
		 double neLat = map.getBounds().getNorthEast().getLatitude();
		 double neLng = map.getBounds().getNorthEast().getLongitude();
		    
		 return "swlat=" + swLat + "&swlng=" + swLng + "&nelat=" + neLat + "&nelng=" + neLng; 
		 
	 }
	
	 void resizeMap(int width, int height) {

		 int mapX = map.getAbsoluteLeft(); 
		 int mapY = map.getAbsoluteTop(); 
		 int newWidth = width - mapX - 2 * MAP_BORDER; 
		 int newHeight = height - mapY - 2 * MAP_BORDER; 
		 map.setSize( newWidth + "px", newHeight + "px"); 
	} 
	
	
	private Marker createMarker(LatLng point, final int number) {
	    final Marker marker = new Marker(point);
	    
	    marker.addMarkerClickHandler(new MarkerClickHandler() {
	      public void onClick(MarkerClickEvent event) {
	        InfoWindow info = map.getInfoWindow();
	        Widget parent = map.getParent();
	        
	        info.open(marker,
	            new InfoWindowContent("Marker #<b>" + number + " left = " + parent.getAbsoluteLeft() + "</b>"));
	      }
	    });
	    
	    return marker;
	  }
	
}
